import expression.*;
import exercise.*;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * JUnit测试类
 * 使用JUnit框架进行单元测试
 */
public class ExpressionJUnitTest {
    
    @Test
    public void testAddExpressionCalculation() {
        AddExpression add = new AddExpression(15, 7);
        assertEquals("15+7应该等于22", 22, add.calculate());
    }
    
    @Test
    public void testSubExpressionCalculation() {
        SubExpression sub = new SubExpression(20, 8);
        assertEquals("20-8应该等于12", 12, sub.calculate());
    }
    
    @Test
    public void testSubExpressionAutoSwap() {
        SubExpression sub = new SubExpression(5, 10);
        assertEquals("5-10应自动交换为10-5=5", 5, sub.calculate());
        assertEquals("交换后左操作数应该是10", 10, sub.getLeft());
        assertEquals("交换后右操作数应该是5", 5, sub.getRight());
    }
    
    @Test
    public void testExpressionInheritance() {
        AddExpression add = new AddExpression(3, 4);
        SubExpression sub = new SubExpression(9, 2);
        
        assertTrue("AddExpression应该是Expression的子类", add instanceof Expression);
        assertTrue("SubExpression应该是Expression的子类", sub instanceof Expression);
    }
    
    @Test
    public void testExerciseAnswerChecking() {
        AddExpression expr = new AddExpression(6, 9);
        Exercise exercise = new Exercise(expr);
        
        exercise.setUserAnswer(15);
        assertTrue("正确答案应该返回true", exercise.isCorrect());
        assertEquals("正确答案应该得1分", 1, exercise.getScore());
        
        exercise.setUserAnswer(10);
        assertFalse("错误答案应该返回false", exercise.isCorrect());
        assertEquals("错误答案应该得0分", 0, exercise.getScore());
    }
    
    @Test
    public void testExerciseSetSizes() {
        ExerciseSet easySet = new ExerciseSet("easy");
        ExerciseSet mediumSet = new ExerciseSet("medium");
        ExerciseSet hardSet = new ExerciseSet("hard");
        
        assertEquals("简单难度应该有10道题", 10, easySet.getExercises().size());
        assertEquals("中等难度应该有15道题", 15, mediumSet.getExercises().size());
        assertEquals("困难难度应该有20道题", 20, hardSet.getExercises().size());
    }
    
    @Test
    public void testExerciseRatio() {
        ExerciseSet easySet = new ExerciseSet("easy");
        long addCount = easySet.getExercises().stream()
            .filter(e -> e.getExpression() instanceof AddExpression)
            .count();
        long subCount = easySet.getExercises().size() - addCount;
        
        assertEquals("简单难度应该有7道加法题", 7, addCount);
        assertEquals("简单难度应该有3道减法题", 3, subCount);
    }
}